package com.ms.oschina.ui.activity;

import java.io.File;
import java.io.IOException;

import android.content.Intent;
import android.graphics.Bitmap;
import android.net.Uri;
import android.os.Environment;
import android.support.v4.app.Fragment;
import android.text.TextUtils;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.ms.oschina.R;
import com.ms.oschina.api.remote.OkHttpClientManager.OnGetNetResultListener;
import com.ms.oschina.api.remote.OsChinaApi;
import com.ms.oschina.base.AppContext;
import com.ms.oschina.base.BaseDetailsActivity;
import com.ms.oschina.base.LoadingPager.ResultCode;
import com.ms.oschina.bean.HttpResponse;
import com.ms.oschina.bean.Result;
import com.ms.oschina.fragment.ExpressionFragment;
import com.ms.oschina.fragment.ShotDetailBottomFragment;
import com.ms.oschina.util.PhotoUtilChange;
import com.ms.oschina.util.UiUtils;

/*
 * 弹一谈
 */
public class ShotActivity extends BaseDetailsActivity {

	protected ResultCode initNetData() {
		return ResultCode.STATUS_SUCCESS;
	}

	private EditText etShotTweetContent;
	private RelativeLayout rlShotTweetImg;
	private ImageView ivTweetImg;
	private ImageView rlShotClear;
	private TextView tvShotTextAstrict;
	private ExpressionFragment expressionFragment;
	private ShotDetailBottomFragment shotDetailBottomFragment;
	private static final int REQ_PICK_CODE = 101;
	private static final int takePhotoCode = 102;
	private static final int zoomCode = 103;
	private File tempFile;
	private String path;
	private String selectImgPath;

	@Override
	public void init() {
		// getWindow().setSoftInputMode(
		// WindowManager.LayoutParams.SOFT_INPUT_ADJUST_PAN);
	}

	private void findViews(View view) {
		etShotTweetContent = (EditText) view
				.findViewById(R.id.et_shot_tweetContent);
		rlShotTweetImg = (RelativeLayout) view
				.findViewById(R.id.rl_shot_tweetImg);
		ivTweetImg = (ImageView) view.findViewById(R.id.iv_shot_tweetImg);
		rlShotClear = (ImageView) view.findViewById(R.id.rl_shot_clear);
		tvShotTextAstrict = (TextView) view
				.findViewById(R.id.tv_shot_textAstrict);
	}

	@Override
	protected CharSequence getBackActionBarTitle() {
		return UiUtils.getString(R.string.actionbar_title_tweetpub);
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		getMenuInflater().inflate(R.menu.send_menu, menu);
		return super.onCreateOptionsMenu(menu);
	}

	@Override
	public boolean onMenuItemSelected(int featureId, MenuItem item) {
		switch (item.getItemId()) {
		case R.id.public_menu_send:
			String text = etShotTweetContent.getText().toString();
			if (TextUtils.isEmpty(text)) {
				UiUtils.showToast("内容不能为null");
			} else {
				// 发送数据
				try {
					String uid = AppContext.getUid();
					OsChinaApi.UploadFile(selectImgPath, uid, text,
							new OnGetNetResultListener<Result>() {
								@Override
								public void onGetNetResult(int requestCode,
										HttpResponse<Result> result) {
									int errorCode = result.errorCode;
									String msg = (String) result.obj;
									if (errorCode == 1) {// 上传成功
										ShotActivity.this.finish();
										UiUtils.showToast("上传成功");
									} else {
										UiUtils.showToast(msg);
									}
								}
							});

				} catch (IOException e) {
					UiUtils.showToast("上传失败");
					e.printStackTrace();
				}
			}
			return true;
		}
		return super.onMenuItemSelected(featureId, item);
	}

	@Override
	protected View loadSuccessView() {
		View view = UiUtils.inflate(R.layout.activity_shot);
		findViews(view);
		setData();
		return view;
	}

	private void setData() {
		shotDetailBottomFragment.setAddEmjoiEditText(etShotTweetContent);
		shotDetailBottomFragment.onSelectorPhotoListener(this);
		rlShotClear.setOnClickListener(this);
		rlShotTweetImg.setVisibility(View.GONE);

		File filesDir = getExternalFilesDir(Environment.DIRECTORY_PICTURES);
		path = filesDir.getAbsoluteFile() + "/" + "temp";
		tempFile = new File(path);
	}

	@Override
	protected boolean onlySuccessView() {
		return true;
	}

	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		if (requestCode == takePhotoCode) {// 得到拍照的图片
			selectImgPath = path;
		} else {
			if (data == null) {
				return;
			}
			Uri selectedImage = data.getData();
			selectImgPath = PhotoUtilChange.getPath(selectedImage, this);

		}
		int dimensionPixelSize = UiUtils
				.getDimensionPixelSize(R.dimen.space_100);
		// 生成略缩图
		Bitmap imageThumbnail = PhotoUtilChange.getImageThumbnail(
				selectImgPath, dimensionPixelSize, dimensionPixelSize);

		ivTweetImg.setImageBitmap(imageThumbnail);
		rlShotTweetImg.setVisibility(View.VISIBLE);
		super.onActivityResult(requestCode, resultCode, data);
	}

	@Override
	protected void processClick(View v) {
		int id = v.getId();
		switch (id) {
		case R.id.ib_shotdetailbottom_gallery:
			PhotoUtilChange.getPhotoDialog(this, takePhotoCode, REQ_PICK_CODE,
					tempFile);
			break;
		case R.id.rl_shot_clear:
			rlShotTweetImg.setVisibility(View.GONE);
			break;
		default:
			break;
		}
	}

	@Override
	protected Fragment bottomControlFragement() {
		shotDetailBottomFragment = new ShotDetailBottomFragment();
		return shotDetailBottomFragment;
	}
}
